package com.trainmodel.eu.z21helper;

import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.Border;

import com.apple.eawt.Application;

public class Z21Helper implements ActionListener {

	private CenterSide centerSide;
	private EmulatorSide emulatorSide;
	private String centerHost = null;

	private JFrame frame;
	private JLabel txLabel;
	private JLabel rxLabel;
	private JLabel centerLabel;
	private JLabel emulatorLabel;
	private JButton exitButton;
	
	public Z21Helper() {
	}

	public Z21Helper(String centerHost) {
		this.centerHost = centerHost;
	}

	void start() {
		this.centerSide = new CenterSide(this.centerHost);
		this.emulatorSide = new EmulatorSide();
		centerSide.setEmulatorSide(emulatorSide);
		emulatorSide.setCenterSide(centerSide);
		
		centerSide.setZ21helper(this);
		emulatorSide.setZ21helper(this);

		(new Thread(centerSide)).start();
		(new Thread(emulatorSide)).start();

		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				createAndShowGUI();
			}
		});

		(new Thread() {
			public void run() {
				while (true) {
					final String tx = "Tx: " + getCenterSide().getTxCounter() + " B/s";
					final String rx = "Rx: " + getCenterSide().getRxCounter() + " B/s";
					getCenterSide().resetCounters();
					javax.swing.SwingUtilities.invokeLater(new Runnable() {
						public void run() {
							txLabel.setText(tx);
							rxLabel.setText(rx);
						}
					});
					if (getEmulatorSide().getEmulatorAddress()!=null) {
						javax.swing.SwingUtilities.invokeLater(new Runnable() {
							public void run() {
								emulatorLabel.setText("Emulator connected!");
							}
						});
					}
					try {
						sleep(1000);
					} catch (InterruptedException e) {
						// do nothing
					}
				}
			}
		}).start();
	}

	public void errorAndQuit(String error) {
		JOptionPane.showMessageDialog(null, error);
		System.exit(1);
	}
	
	private void createAndShowGUI() {
		System.setProperty("apple.laf.useScreenMenuBar", "true");
		JFrame.setDefaultLookAndFeelDecorated(true);
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e) {
			// No means no, no probles here
		}
		Application application = null;
		try {
			application = (Application) Class.forName("com.apple.eawt.Application").newInstance();
		} catch (Exception e) {
			// Not OS X, everything ok
		}
		Image image = Toolkit.getDefaultToolkit().getImage(getClass().getResource("/icons/icon.png"));
		if (application != null)
			application.setDockIconImage(image);
		frame = new JFrame("Z21 Helper");
		frame.setResizable(false);
		frame.setIconImage(new ImageIcon(getClass().getResource("/icons/icon.png")).getImage());
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Border padding = BorderFactory.createEmptyBorder(10, 5, 10, 5);
		JPanel contentPanel = new JPanel();
		contentPanel.setBorder(padding);
		frame.setContentPane(contentPanel);
		frame.setLayout(new BoxLayout(contentPanel, BoxLayout.Y_AXIS));

		centerLabel = new JLabel("Z21 Center: " + getCenterSide().getCenterHost());
		centerLabel.setHorizontalAlignment(SwingConstants.LEFT);
		frame.getContentPane().add(centerLabel);

		emulatorLabel = new JLabel("Waiting for emulator...                   ");
		emulatorLabel.setHorizontalAlignment(SwingConstants.LEFT);
		frame.getContentPane().add(emulatorLabel);
		
		txLabel = new JLabel("Tx: ---------------------");
		txLabel.setHorizontalAlignment(SwingConstants.RIGHT);
		frame.getContentPane().add(txLabel);
		
		rxLabel = new JLabel("Rx: ---------------------");
		rxLabel.setHorizontalAlignment(SwingConstants.RIGHT);
		frame.getContentPane().add(rxLabel);
		
		frame.getContentPane().add(new JLabel(" "));
		
		
		exitButton = new JButton("Quit");
		exitButton.setHorizontalAlignment(SwingConstants.CENTER);
		exitButton.setFocusable(false);
		exitButton.setDefaultCapable(false);
		// exitButton.setBorder(BorderFactory.createEmptyBorder(10, 0, 5, 5));
		exitButton.addActionListener(this);
		frame.getContentPane().add(exitButton);
		

		frame.pack();
		frame.setVisible(true);
	}

	public CenterSide getCenterSide() {
		return centerSide;
	}

	public void setCenterSide(CenterSide centerSide) {
		this.centerSide = centerSide;
	}

	public EmulatorSide getEmulatorSide() {
		return emulatorSide;
	}

	public void setEmulatorSide(EmulatorSide emulatorSide) {
		this.emulatorSide = emulatorSide;
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		if (arg0.getSource()==exitButton) {
			System.out.println("Bye bye");
			System.exit(0);
		}
		
	}

}
